package sivak.net.proc.dyn.single_lib_2;

import com.sun.org.apache.xalan.internal.xsltc.dom.NodeSortRecord;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 1/23/13
 * Time: 3:49 PM
 * To change this template use File | Settings | File Templates.
 */
public class SingleLLUtils_2 {
    public static Node add(Node tail, int elem) {
        if (tail == null)
            return new Node(elem, null);
        Node current = tail;
        while (current.next != null)
            current = current.next;
        current.next = new Node(elem, null);
        return tail;
    }

    public static Node add(Node tail, int index, int elem) {
        if (index == 1) {
            return new Node(elem, tail);
        }
        Node current = tail;
        for (int k = 1; k < index - 1; k++)
            current = current.next;
        current.next = new Node(elem, current.next);
        return tail;

    }

    public static Node remove(Node tail) {
        if (tail == null)
            throw new IllegalArgumentException();
        if (tail.next == null)
            return null;
        Node current = tail;
        while (current.next.next != null)
            current = current.next;
        current.next = null;
        return tail;
    }

    public static Node remove(Node tail, int index) {
        if (tail == null)
            throw new IllegalArgumentException();
        Node current = tail;
        for (int k = 1; k < index - 1; k++) {
            current = current.next;
        }
        current.next = current.next.next;
        return tail;
    }
}
